package com.seecoder.BlueWhale.po;

import com.seecoder.BlueWhale.enums.DeliveryMethodEnum;
import com.seecoder.BlueWhale.enums.OrderStatusEnum;
import com.seecoder.BlueWhale.vo.OrderVO;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import javax.persistence.*;
import java.util.Date;

@Entity
@Setter
@Getter
@NoArgsConstructor
@Table(name = "`order`", indexes = {
        @Index(columnList = "owner_id"),
        @Index(columnList = "store_id"),
        @Index(columnList = "status"),
        @Index(columnList = "create_time")
})
public class Order {
    private static final Logger log = LoggerFactory.getLogger(Order.class);
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    @Column(name = "id")
    private Integer id;                          //订单编号

    @Basic
    @Enumerated(EnumType.STRING)
    @Column(name = "status")
    private OrderStatusEnum status;                   //订单状态

    @Basic
    @Column(name = "owner_id")
    private Integer ownerID;                     //订单所有者的id

    @ManyToOne
    @JoinColumn(name = "owner_id", referencedColumnName = "id", insertable = false, updatable = false)
    private User owner;

    @Basic
    @Column(name = "store_id")
    private Integer storeID;                     //订单所属商店的id

    @ManyToOne
    @JoinColumn(name = "store_id", referencedColumnName = "id", insertable = false, updatable = false)
    private Store store;

    @Basic
    @Column(name = "commodity_id")
    private Integer commodityID;                 //订单商品的id

    @ManyToOne
    @JoinColumn(name = "commodity_id", referencedColumnName = "id", insertable = false, updatable = false)
    private Commodity commodity;


    @Basic
    @Column(name = "quantity")
    private Integer quantity;                    //订单商品的数量

    @Basic
    @Column(name = "create_time")
    private Date createTime;                     //创建时间

    @Basic
    @Column(name = "delivery_method")
    @Enumerated(EnumType.STRING)
    private DeliveryMethodEnum deliveryMethod;   //送货方式

    @PostConstruct
    public void init() {
        log.debug("Order init() is {}", this);
    }

    public OrderVO toVO() {
        OrderVO orderVO = new OrderVO();
        orderVO.setId(this.id);
        orderVO.setStatus(this.status);
        orderVO.setOwnerID(this.ownerID);
        orderVO.setStoreID(this.storeID);
        orderVO.setCommodityID(this.commodityID);
        orderVO.setQuantity(this.quantity);
        orderVO.setCreateTime(this.createTime);
        orderVO.setDeliveryMethod(this.deliveryMethod);
        return orderVO;
    }
}
